package com.foreveross.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.TenementService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.JSONUtil;
import com.foreveross.vo.Tenement;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;


@Controller
@RequestMapping(value="/sysTenementController")
public class SysTenementController  extends BaseCostroller {
	
	private Logger log = Logger.getLogger(SysTenementController.class);
	
	@Resource
	private TenementService tenementService;
	
	
	@Resource
	private LogService logService;

	/**
	 * 物业公司信息查询
	 * @param request
	 * @param response
	 * @param name
	 * @param phone
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method=RequestMethod.POST, value="/getTenement")
	public void getTenement(HttpServletRequest request,HttpServletResponse response,
			String house_id,String name,String  phone,String rows,String page,String userId){   
		ResultVo resultVo = new ResultVo();
		try{
			int countRecord = (int)tenementService.getUserListCount(house_id ,name, phone,userId);
			PageVo<Tenement> pageObj = new PageVo<Tenement>(page, countRecord,rows);
			pageObj.setRows(tenementService.findTenementList(house_id ,name, phone, pageObj.getpageIndex(), pageObj.getpageSize(),userId));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("物业公司信息 查看成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("物业公司信息 查看失败");
			log.error("物业公司信息 查看失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 物业公司信息添加修改
	 * @param request
	 * @param response
	 * @param tenement
	 * @param updateType
	 */
	@RequestMapping(method=RequestMethod.POST, value="/addOrUpdateTenement")
	public void addOrUpdateTenement(HttpServletRequest request,HttpServletResponse response,
			String tenementJson,String updateType){    
		ResultVo resultVo = new ResultVo();
		try{
			Tenement tenement = JSONUtil.jsonToClazz(tenementJson, Tenement.class);
			tenementService.saveOrUpdate(tenement, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,updateType.equals(Constants.UPDATE_ADD)?Constants.LOG_ADD:Constants.LOG_UPDATE, "物业公司信息"+Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request), 
					Constants.getUpdateType(updateType)+"物业公司信息[" + tenement.toString()
							+ "]");
			log.info("物业公司信息 "+updateType+"成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("物业公司信息 "+updateType+"失败");
			log.error("物业公司信息"+updateType+"失败!",e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 查看物业详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	
	@RequestMapping(method=RequestMethod.POST, value="/findTenement")
	public void findTenement(HttpServletRequest request,HttpServletResponse response,String id){   
		ResultVo resultVo = new ResultVo();
		try {
			Tenement hrHouses = (Tenement) this.findObject(
					Tenement.class, id);
			hrHouses.setHouse_id(hrHouses.getHouseId().getId());
			hrHouses.setHouse_name(hrHouses.getHouseId().getName());
			resultVo.setSuccess(true);
			resultVo.setData(hrHouses);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看物业详情失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/getTenementSelect")
	public void getTenementSelect(HttpServletRequest request,
			HttpServletResponse response,String userId,String id) {
		List<Tenement> tenementList = null;
		try {
			tenementList = tenementService.getTenementSelect(userId, id, 0, Constants.PAGE_MAX_SIZE);
			log.info("商户管理  查看成功!");
		} catch (Exception e) {
			log.error("商户管理 查看失败!", e);
		}
		CommonUtil.PrintWriterObjectWithJSON(response, tenementList);
	}
 
	
	/**
	 * 物业公司信息删除
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method=RequestMethod.POST, value="/deleteTenementList")
	public void deleteTenementList(HttpServletRequest request,HttpServletResponse response,
			String ids){    
		ResultVo resultVo = new ResultVo();
		try{
			tenementService.deleteTenementList(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "物业公司信息删除",
					AuthUtil.getUserid(request), 
					"物业公司信息[" + ids+ "]");
			log.info("物业公司信息 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage(Constants.SYS_BASEDATA_DEL_ERRE);
			log.info("物业公司信息 删除失败!");
			e.printStackTrace();
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
